package com.sicau.smarttraceplatform.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sicau.smarttraceplatform.common.constants.CommonURLConstants;
import com.sicau.smarttraceplatform.common.returning.ResultData;
import com.sicau.smarttraceplatform.entity.Category;
import com.sicau.smarttraceplatform.entity.Trace;
import com.sicau.smarttraceplatform.entity.User;
import com.sicau.smarttraceplatform.entity.param.category.CategoryUpdateParam;
import com.sicau.smarttraceplatform.service.CategoryService;
import com.sicau.smarttraceplatform.service.TraceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

import static com.sicau.smarttraceplatform.common.constants.commonConstants.CURRENT_USER;

/**
 * <p>
 * 类别 前端控制器
 * </p>
 *
 * @author sxt && ttxxi
 * @since 2020-05-24
 */
@RestController
@RequestMapping(CommonURLConstants.CATEGORY_PREFIX)
@Api(tags = "类别信息")
public class CategoryController {

    @Resource
    private CategoryService categoryService;
    @Resource
    private TraceService traceService;

    @GetMapping({"","/h5"})
    @ApiOperation("查询农产品分类")
    public ResultData getCategory() {
        List<Category> list = categoryService.list();
        return new ResultData<>().OK().setObj(list);
    }

    @PostMapping()
    @ApiOperation("新增农产品分类")
    @ApiImplicitParam(name = "categoryName", value = "分类名称")
    public ResultData addCategory(@ApiParam(hidden = true) HttpServletRequest request,
                                  String categoryName) {
        if (StringUtils.isEmpty(categoryName)){
            return new ResultData().FAILED().setResultIns("参数不能为空");
        }
        List<Category> list = categoryService.list(new LambdaQueryWrapper<Category>().eq(Category::getCategory, categoryName));
        if (list!=null && list.size()>=1){
            return new ResultData().FAILED().setResultIns("分类名称重复");
        }
        Category category = new Category();
        User user = (User) request.getSession().getAttribute(CURRENT_USER);
        category.setCreator(user != null ? user.getId().toString() : null)
                .setCreated(LocalDateTime.now())
                .setCategory(categoryName);
        categoryService.save(category);
        return new ResultData().OK();
    }

    @PutMapping()
    @ApiOperation("修改农产品分类")
    public ResultData updateCategory(@ApiParam(hidden = true) HttpServletRequest request,
                                     @Valid CategoryUpdateParam categoryUpdateParam) {
        Category category = categoryService.getById(categoryUpdateParam.getId());
        if (category==null){
            return new ResultData().FAILED().setResultIns("categoryId错误");
        }
        User user = (User) request.getSession().getAttribute(CURRENT_USER);
        category.setMender(user != null ? user.getId().toString() : null)
                .setModified(LocalDateTime.now())
                .setCategory(categoryUpdateParam.getCategoryName());
        categoryService.save(category);

        //同时更新trace
        traceService.update(new LambdaUpdateWrapper<Trace>()
                .set(Trace::getCategory, categoryUpdateParam.getCategoryName())
                .eq(Trace::getCategoryId, categoryUpdateParam.getId())
        );

        return new ResultData().OK();
    }

    @DeleteMapping()
    @ApiOperation("删除农产品分类")
    @ApiImplicitParam(name = "id", value = "分类id")
    public ResultData updateCategory(String categoryId) {
        categoryService.removeById(categoryId);
        return new ResultData().OK();
    }

}

